<?php

/**
 * ECSHOP 找回密码
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: niqingyang $
 * $Id: findPwd.php 17217 2015-07-27 06:29:08Z niqingyang $
 */
define('IN_ECS', true);

require (dirname(__FILE__) . '/includes/init.php');
/* 载入语言文件 */
require_once (ROOT_PATH . 'languages/' . $_CFG['lang'] . '/user.php');

$ui_arr = array();

$ui_arr[] = 'default';

$affiliate = unserialize($GLOBALS['_CFG']['affiliate']);
$smarty->assign('affiliate', $affiliate);
$back_act = '';
if(!$_SESSION['user_id']){
    ecs_header("Location: user.php\n");
    exit;
}
$action = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'default';

/* 如果是显示页面，对页面进行相应赋值 */
if(in_array($action, $ui_arr) || true)
{
    assign_template();
    $position = assign_ur_here(0, $_LANG['user_center']);
    $smarty->assign('page_title', $position['title']); // 页面标题
    $smarty->assign('ur_here', $position['ur_here']);
    $sql = "SELECT value FROM " . $ecs->table('shop_config') . " WHERE id = 419";
    $row = $db->getRow($sql);
    $car_off = $row['value'];
    $smarty->assign('car_off', $car_off);
    /* 是否显示积分兑换 */
    if(! empty($_CFG['points_rule']) && unserialize($_CFG['points_rule']))
    {
        $smarty->assign('show_transform_points', 1);
    }
    $smarty->assign('helps', get_shop_help()); // 网店帮助
    $smarty->assign('data_dir', DATA_DIR); // 数据目录
    $smarty->assign('action', $action);
    $smarty->assign('lang', $_LANG);
}

$function_name = 'action_' . $action;

if(! function_exists($function_name))
{
    $function_name = "action_default";
}

call_user_func($function_name);

return;

/**
 * 找回密码首页
 */
function action_default ()
{
    $smarty = $GLOBALS['smarty'];
    $db = $GLOBALS['db'];
    $ecs = $GLOBALS['ecs'];
    
    
    
    $smarty->assign("action", "step_1");
    
    $sqlmobile_phone = "SELECT mobile_phone FROM " .$GLOBALS['ecs']->table('users'). " WHERE user_id = '$_SESSION[user_id]'";
    
    $mobile_phoneinfo = $GLOBALS['db']->getone($sqlmobile_phone);
    $smarty->assign("mobile_phoneinfo", $mobile_phoneinfo);
    $smarty->display('user_findtel.dwt');
}

function action_zhaohui_pwd()
{
    $smarty = $GLOBALS['smarty'];
    $db = $GLOBALS['db'];
    $ecs = $GLOBALS['ecs'];
    
    $smarty->assign("action", "step_1");
    
    $sqlmobile_phone = "SELECT mobile_phone FROM " .$GLOBALS['ecs']->table('users'). " WHERE user_id = '$_SESSION[user_id]'";
    
    $mobile_phoneinfo = $GLOBALS['db']->getone($sqlmobile_phone);
    $smarty->assign("mobile_phoneinfo", $mobile_phoneinfo);
    $smarty->display('user_zhaohuipwd.dwt');
}

function action_to_reset_password ()
{
    $user = $GLOBALS['user'];
    $_CFG = $GLOBALS['_CFG'];
    $_LANG = $GLOBALS['_LANG'];
    $smarty = $GLOBALS['smarty'];
    $db = $GLOBALS['db'];
    $ecs = $GLOBALS['ecs'];
    $user_id = $_SESSION['user_id'];
    
 
    $mobile_phone =!empty($_POST['mobile']) ? trim($_POST['mobile']) : '';
    $mobile_phone2 =!empty($_POST['mobile2']) ? trim($_POST['mobile2']) : '';

    
  
    
    if(!$_POST['mobile_code']){
        show_message('请填写手机验证码');
    }
   
    if($_POST['mobile_code']!=$_SESSION['yzm_code']){
        show_message('手机验证码不正确');
    }
    $chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|19[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$/";
    if(preg_match($chars, $mobile_phone))
    {
        $sqlmobile_phone = "SELECT mobile_phone FROM " .$GLOBALS['ecs']->table('users'). " WHERE mobile_phone = '$mobile_phone'";
        
        $mobile_phoneinfo = $GLOBALS['db']->getAll($sqlmobile_phone);
        if($mobile_phoneinfo){
            show_message('新手机号已经存在,请重新输入');
        }
    }else{
        show_message('请输入正确的手机号');
    }
    if($mobile_phone!=$mobile_phone2){
        show_message('两次输入手机号不一致，请重新输入');
    }
    
    
    
    $sql = 'UPDATE ' . $GLOBALS['ecs']->table('users') .
    " SET mobile_phone = '" .$mobile_phone. "'" .
    " WHERE user_id = '$user_id' LIMIT 1";
    $res=$GLOBALS['db']->query($sql);
    if($res){
        unset($_SESSION['yzm_code']);
        unset($_SESSION['yzm_tel']);
        show_message('手机号码更改成功');
    }else{
        show_message('手机号码更改失败');
    }
   
}
function action_to_reset_pwd()
{
    $user = $GLOBALS['user'];
    $_CFG = $GLOBALS['_CFG'];
    $_LANG = $GLOBALS['_LANG'];
    $smarty = $GLOBALS['smarty'];
    $db = $GLOBALS['db'];
    $ecs = $GLOBALS['ecs'];
    $user_id = $_SESSION['user_id'];
    if($_POST['mobile_code']!=$_SESSION['yzm_code']){
        show_message('手机验证码不正确');
    }
    ecs_header("Location: user.php?act=qianbao_pwd&telzh=1\n");
    exit;
    
}

function action_get_code ()
{
    $user = $GLOBALS['user'];
    $_CFG = $GLOBALS['_CFG'];
    $_LANG = $GLOBALS['_LANG'];
    $smarty = $GLOBALS['smarty'];
    $db = $GLOBALS['db'];
    $ecs = $GLOBALS['ecs'];
    $user_id = $_SESSION['user_id'];
    require_once (ROOT_PATH . 'sms/sms.php');
    
    
    
    $sqlmobile_phone = "SELECT mobile_phone FROM " .$GLOBALS['ecs']->table('users'). " WHERE mobile_phone = '$_GET[mobile]'";
    
    $mobile_phoneinfo = $GLOBALS['db']->getAll($sqlmobile_phone);
    
    //if(!$_GET['zfmm']){
       // if($mobile_phoneinfo){
          //  echo 3;
           // exit;
        //}
   // }
    
    
    // 生成6位短信验证码
    $mobile_code = rand_number(4);
  
    // 短信内容
    $content = sprintf($_LANG['mobile_code_template'], $GLOBALS['_CFG']['shop_name'], $mobile_code, $GLOBALS['_CFG']['shop_name']);
  
    /* 发送激活验证邮件 */
    // $result = true;
    $result = sendSMS($_GET['mobile'], $content);//byking 发送短信
    if($result){
        $_SESSION['yzm_code']=$mobile_code;
        $_SESSION['yzm_tel']=$_GET['mobile'];
      
        echo 1;
    }else{
        echo 2;
    }
    exit;
}

/**
 * 随机生成指定长度的数字
 *
 * @param number $length
 * @return number
 */
function rand_number ($length = 6)
{
    if($length < 1)
    {
        $length = 6;
    }
    
    $min = 1;
    for($i = 0; $i < $length - 1; $i ++)
    {
        $min = $min * 10;
    }
    $max = $min * 10 - 1;
    
    return rand($min, $max);
}

?>